Ext.define('PJ.view.trainingmanagement.Add', {
	extend: 'AM.default.DefaultWindowForm',
	alias: ['widget.trainingManagementEdit'],
	requires: ['AM.default.DefaultDateField', 
	           'AM.default.DateTime',
	           'AM.default.jsonp.DefaultSubmitJsonp',
	           'AM.default.DefaultDateField',
	           'PJ.view.trainingmanagement.CourseTree',
	           'PJ.view.trainingmanagement.CourseList'//课程列表
	           ],
	modal: true,// 开启不允许点击其他组件
	frame: true,
	notEdit: true,
	width: 650,
	height:470,
	layout: 'form',
	isSame: false,
	overflowY:'auto',
//	autoScroll:true,
	initComponent: function(){
    	var me = this;
    	var bt = '<span style="color:red;">*</span>';
    	if(me.flag == 4 ){
    		bt = ""
    	}
        var theDate = new Date();
        theDate.setFullYear(theDate.getFullYear(), 0, 1);
    	me.items= [{
    		defaults: {
    			labelAlign: 'right',
    			labelWidth: 90,
    			columnWidth: .5,
    			padding:"9 0 0 0",
    			readOnly:me.isView
    		},
    		border: false,
    		xtype: "container",
    		width: 610,
    		height:300,
    		layout: 'column',
    		allowBlank: false,
    		items: [{
    			xtype: 'textfield',
    			fieldLabel:me.flag==3?'主办单位':bt+'主办单位',
    			name: 'sponsorName',
    			columnWidth:.4,
    			allowBlank: true,
    			readOnly:true,
    			margin: "5 0 0 0",
    			value: me.selectScopeStr,
    			disabled:me.flag==3?true:false,
    		},{
            	xtype: 'combo',
            	fieldLabel: bt + '培训班类型',
    			columnWidth:.3,
                name: 'classType',
                allowBlank: false,
                margin: "5 0 0 0",
                queryMode: 'local',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: 'ajax',
                        url: me.grid.store.trainingClassTypeUrl + '?typeCode=pxblx',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                editable: false,
                defaultActive: false,
                emptyText: '请选择'
            },{
            	xtype: 'combo',
            	fieldLabel: bt + '年度',
                name: 'classnameYear',
                margin: "5 0 0 0",
                value:theDate.getFullYear(),
    			columnWidth:.3,
                allowBlank: false,
                queryMode: 'local',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: 'ajax',
                        url: me.grid.store.trainingClassTypeUrl + '?typeCode=nd',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                editable: false,
                defaultActive: false,
                emptyText: '请选择'
            },{
    			xtype: 'textfield',
    			fieldLabel:me.flag==3?'培训班名称': bt+'培训班名称',
    			disabled:me.flag==3?true:false,
    			name: 'className',
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
                },
    			columnWidth:.7,
                maxLength: 50
    		},{
    			xtype: 'numberfield',
    			fieldLabel: bt+'天数',
    			name: 'trainingDay',
    			id: 'trainingDay',
    			disabled:me.flag==3?true:false,
    	        queryMode: 'local',
    			allowBlank: me.isReadOnly,
    			readOnly:false,
				hideTrigger: true,
				vtype: 'blankTrim',
				minValue:0.01,
				maxValue: 99999,
				decimalPrecision: 2,
    			columnWidth:.3,
				value:1,
    			listeners: {
                	change: function(textField){
                		var baseForm = me.down("form").getForm();
                		baseForm.findField("trainingHour").setValue(textField.getValue()*8);
                	}
                }
    		},{
            	xtype: 'combo',
            	fieldLabel: bt + '培训类别',
                name: 'categoryId',
                allowBlank: false,
                queryMode: 'local',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: 'ajax',
                        url: me.grid.store.trainingClassTypeUrl + '?typeCode=pxlb',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                editable: false,
                defaultActive: false,
    			columnWidth:.35,
                emptyText: '请选择'
            },{
            	xtype: 'combo',
            	fieldLabel: bt + '离岗状态',
                name: 'beoffStation',
                allowBlank: false,
                queryMode: 'local',
                value:'tc',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: 'ajax',
                        url: me.grid.store.trainingClassTypeUrl + '?typeCode=lgzt',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                editable: false,
                defaultActive: false,
    			columnWidth:.35,
                emptyText: '请选择'
            },{
    			xtype: 'numberfield',
    			fieldLabel: bt+'学时',
    			name: 'trainingHour',
    			id: 'trainingHour',
    			disabled:me.flag==3?true:false,
    			allowBlank: me.isReadOnly,
    			readOnly:false,
				hideTrigger: true,
				vtype: 'blankTrim',
				minValue:0.1,
				maxValue: 99999,
				decimalPrecision: 1,
    			columnWidth:.3,
				value:8,
    			listeners: {
                	change: function(textField){
                		var baseForm = me.down("form").getForm();
                		baseForm.findField("trainingDay").setValue(textField.getValue()/8);
                	}
                }
    		},/*{
    			xtype: 'textfield',
    			fieldLabel: bt+'培训班编号',
    			name: 'classNum',
    			//allowDecimals:false,
    			//hideTrigger: true,
    			//maxValue: 9999999,
    			value:me.classNum,
    			readOnly:false,
    			//readOnly:true,
    			allowBlank: me.isReadOnly,
    			disabled:me.flag==3?true:false,
    			emptyText: '请输入9位数字或字母',
    			regex: /^[A-Za-z0-9]{9}$/,
    			regexText: '培训班编号只能输入9位长度的数字或字母！'
    		},*/{
    			xtype: 'textfield',
    			fieldLabel:me.flag==3?'承办单位':'承办单位',
    			name: 'scopeDepartmentNames',
    			allowBlank: true,
    			readOnly:true,
    			disabled:me.flag==3?true:false,
    			columnWidth:me.isReadOnly ? .5 : .38,
    			columnWidth: 0.88
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '承办单位',
    			margin: "9 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"configIco",
    			hidden:me.isReadOnly,
    			handler: function(button){
    				var  baseForm = this.up('form').getForm();
    				var scopeDepartmentNames = baseForm.findField('scopeDepartmentNames');
    				var scopeDepartmentIds = baseForm.findField('scopeDepartmentIds');
    				Ext.create("PJ.view.trainingmanagement.WindowFrame", {
                        title: "选取承办单位",
                        edit:this,
                        scopeDepartmentNames:scopeDepartmentNames,
                        scopeDepartmentIds:scopeDepartmentIds
                    }).show();
    			}
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '清除',
    			margin: "9 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"deleteIco",
    			hidden:me.isReadOnly,
    			//disabled: me.isReadOnly,
    			handler: function(button){
    				var form = button.up("form");
    				var baseForm = form.getForm();
    				baseForm.findField("scopeDepartmentNames").setValue("");
    				baseForm.findField("scopeDepartmentIds").setValue("");
    			}
    		},{
    			xtype: 'textfield',
    			fieldLabel:'指定报名单位',
    			name: 'signUpDepartmentNames',
    			allowBlank: true,
    			readOnly:true,
    			disabled:me.flag==3?true:false,
    			//value: me.selectScopeStr,
    			columnWidth:me.isReadOnly ? .5 : .38,
    			columnWidth: 0.88
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '指定报名单位',
    			margin: "9 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"configIco",
    			hidden:me.isReadOnly,
    			handler: function(button){
    				var  baseForm = this.up('form').getForm();
    				var signUpDepartmentNames = baseForm.findField('signUpDepartmentNames');
    				var signUpDepartmentIds = baseForm.findField('signUpDepartmentIds');
    				Ext.create("PJ.view.trainingmanagement.WindowFrame2", {
                        title: "选取报名单位",
                        edit:this,
                        signUpDepartmentNames:signUpDepartmentNames,
                        signUpDepartmentIds:signUpDepartmentIds
                    }).show();
    			}
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '清除',
    			margin: "9 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"deleteIco",
    			hidden:me.isReadOnly,
    			//disabled: me.isReadOnly,
    			handler: function(button){
    				var form = button.up("form");
    				var baseForm = form.getForm();
    				baseForm.findField("signUpDepartmentNames").setValue("");
    				baseForm.findField("signUpDepartmentIds").setValue("");
    			}
    		},/*{
    			xtype: 'numberfield',
    			fieldLabel: '班级人数',
    			name: 'classUserNum',
    			allowDecimals:false,
    			hideTrigger: true,
    			disabled:me.flag==3?true:false,
    			readOnly:true,
    			allowBlank: true,
    			value: 0
    		},*/
    		
    		{
				xtype: 'defaultDate',
				fieldLabel:bt+'开班时间',
    			allowBlank: me.isReadOnly,
				name: 'beginDateStr',
				id: 'questionnaireSD',
				vtype: 'startLessEndDate',
				startLessEndDate: {
					targetCmpId: 'questionnaireED',
					isStart: true
				},
				margin: '5 0 0 0',
				emptyText: me.flag==4?"":"请输入开班时间",
    			readOnly:me.isReadOnly,
    			value: theDate,
    			listeners: {
//                	change: function(textField){
//                		var baseForm = me.down("form").getForm();
//                		var startDateStr = textField.getValue();
//                		var endStr = baseForm.findField("endDateStr").getValue();
//                		var startTime = startDateStr.getTime();
//                		var endTime = endStr.getTime();
//                		var num = (endTime - startTime)/1000/60/60/24;
//                		baseForm.findField("trainingHour").setValue((num+1)*8);
//                	}
                }
			},
			{
				xtype: 'defaultDate',
				fieldLabel: bt+'结业时间',
    			allowBlank: me.isReadOnly,
				name: 'endDateStr',
				id: 'questionnaireED',
				vtype: 'startLessEndTwoDate',
				vtypeText: '结业时间不能早于开班时间与报名开始时间！',
				startLessEndDate: {
					targetCmpId: 'questionnaireSD',
					targetCmpTwoId: 'registrationStartsSD',
					isStart: false
				},
				margin: '5 0 0 0',
				emptyText:  me.flag==4?"":"请输入结业时间",
				allowBlank: false,
    			readOnly:me.isReadOnly,
    			value: theDate,
    			listeners: {
                	change: function(textField){
//                		var baseForm = me.down("form").getForm();
//                		var endDateStr = textField.getValue();
//                		var startTimeStr = baseForm.findField("beginDateStr").getValue();
//                		var endTime = endDateStr.getTime();
//                		var startTime = startTimeStr.getTime();
//                		var num = (endTime - startTime)/1000/60/60/24;
//                		baseForm.findField("trainingHour").setValue((num+1)*8);
                	}
                }
			},
    		
    		{
				xtype: 'defaultDate',
				fieldLabel:'报名开始时间',
				name: 'enterBeginTimeStr',
				
				id: 'registrationStartsSD',
				vtype: 'startLessEndTwoDate',
				vtypeText: '报名开始时间不能早于结业时间预报名截止时间！',
				startLessEndDate: {
					targetCmpId: 'questionnaireED',
					targetCmpTwoId: 'applicationED',
					isStart: true
				},
				margin: '5 0 0 0',
				emptyText: me.flag==4?"":"请输入报名开始时间",
//				disabled:me.isDisable,
    			allowBlank:me.isDisable?true:false,
    			readOnly:me.isReadOnly
			}, {
				xtype: 'defaultDate',
				fieldLabel: '报名截止时间',
				name: 'enterEndTimeStr',
				id: 'applicationED',
				vtype: 'startLessEndDate',
				vtypeText: '报名截止时间不能早于报名开始时间！',
				startLessEndDate: {
					targetCmpId: 'registrationStartsSD',
					isStart: false
				},
				margin: '5 0 0 0',
				emptyText: me.flag==4?"":"请输入报名截止时间",
//				disabled:me.isDisable,
    			allowBlank: me.isDisable?true:false,
    			readOnly:me.isReadOnly
			},{
            	xtype: 'combo',
            	fieldLabel: bt+'培训机构',
                name: 'trainingDeptId',
                queryMode: 'local',
                store: Ext.create("Ext.data.ArrayStore", {
                    singleton: true,
                    fields: [{
                        name: 'value',
                        mapping: "dataCode",
                        type: 'string'
                    }, {
                        name: 'text',
                        mapping: "dataName",
                        type: 'string'
                    }],
                    proxy: {
                        type: 'ajax',
                        url: me.grid.store.trainingClassTypeUrl + '?typeCode=pxjg',
                        reader: Ext.create('Ext.data.reader.Json', {
                            type: 'json',
                            root: 'data',
                            successProperty: 'success'
                        })
                    },
                    autoLoad: true
                }),
                valueField: 'value',
                displayField: 'text',
                margin: "5 0 0 0",
                editable: false,
    			allowBlank: me.isReadOnly,
                emptyText: '请选择',
                listeners: {
                	change: function(textField){
                		var baseForm = me.down("form").getForm();
                		var value = textField.value;
            			baseForm.findField("trainingDeptName").setValue("");
                		if(value == 'qtjjdwdx'){
                    		baseForm.findField("trainingDeptName").setDisabled(false);
                		}else if(value == 'qwdx'){
                			baseForm.findField("trainingDeptName").setDisabled(false);
                		}else if(value == 'ptgdyx'){
                			baseForm.findField("trainingDeptName").setDisabled(false);
                		}else if(value == 'jwpxjg'){
                			baseForm.findField("trainingDeptName").setDisabled(false);
                		}else if(value == 'qtzyppxjg'){
                			baseForm.findField("trainingDeptName").setDisabled(false);
                		}else if(value == 'qtpxjg'){
                			baseForm.findField("trainingDeptName").setDisabled(false);
                		}else{
                			baseForm.findField("trainingDeptName").setValue(this.rawValue);
                			baseForm.findField("trainingDeptName").setDisabled(true);
                		}
                	}
                }
            },{
    			xtype: 'textfield',
    			fieldLabel: bt+'培训机构名称',
    			name: 'trainingDeptName',
    			disabled:true,
    			allowBlank: me.isReadOnly,
    			readOnly:false,
    			hidden: false,
    			vtype: 'blankTrim',
    			margin: "5 0 0 0",
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
                },
                maxLength: 50
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'培训地点',
    			name: 'trainingAddress',
    			columnWidth: 1,
    			vtype: 'blankTrim',
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 150){
                			Orchid.alert('提示','该输入项最多输入150个字符！');
                		}
                	}
                },
                maxLength: 150
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'联系人',
    			name: 'contactPeople',
    			maxLength:30,
    			/*disabled:me.isDisable,*/
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
    			listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
    			}
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'手机',
    			name: 'contactTel',
    			hideTrigger: true,
    			//regex: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
				//regexText : '请输入正确的联系电话',
    			/*disabled:me.isDisable,*/
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
    			maxLength: 50,
    			listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
    			}
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'固定电话',
    			hideTrigger: true,
    			name: 'phone',
    			//regex: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
				//regexText : '请输入正确的传真号',
    			/*disabled:me.isDisable,*/
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
    			maxLength: 50,
    			listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
    			}
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'传真',
    			hideTrigger: true,
    			name: 'fax',
    			//regex: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
				//regexText : '请输入正确的传真号',
    			/*disabled:me.isDisable,*/
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
    			maxLength: 50,
    			listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
    			}
    		},{
    			xtype: 'hidden',
    			name: 'sponsorID',//主板部委Id
				value: me.selectScopeId
    		},{
    			xtype: 'hidden',
    			name: 'scopeDepartmentIds'//承办部委IDs
    		},{
    			xtype: 'hidden',
    			name: 'signUpDepartmentIds',//指定报名机构Id
				//value: me.selectScopeId
    		},{
    			xtype: 'hidden',
    			name: 'classId'//培训班Id
    		}/*,{
    			xtype: 'hidden',//培训班编号
    			name: 'classNum'
    		}*/]
    	},{
    		xtype: "container",
			layout: 'column',
			defaults: {
    			labelAlign: 'right',
    			labelWidth: 90,
    			columnWidth: .5,
    			padding:"9 0 0 0",
    			readOnly:me.isView
    		},
			items: [{
				xtype: 'pfCourseList',
				columnWidth: .98,
				hidden:me.flag==4?false:true,
				classId: me.classId,
				padding: '0 0 0 0',
				height: 271
			}]
		}];
		this.buttons = [{
			text:"确定",
			scope:this,
			hidden:me.isReadOnly,
			handler:function(btn){
				var win = me;
				var form = win.down('form');
				var baseForm = form.getForm();
				var classNum_ = baseForm.findField("classNum_");
				if(classNum_ != null && classNum_.getValue() != null && classNum_ != ""){
					baseForm.findField("classNum").setValue(classNum_.getValue());
				}
				if (!baseForm.isValid()) {
					return;
				}
				var url = "";
				
				var params = {};
				if(me.flag==1){				//1 新增  2 修改   3 完善
					url = me.grid.store.addUrl;
				}else if(me.flag==2){
					url = me.grid.store.updateUrl;
				}else if(me.flag==3){
					url = me.grid.store.derfectUrl;
					params = {
						className: win.className,
						ministrieName: win.ministrieName
					};
				}
				
				if(gj_onlineLearning_constant.loadType == "jsonp"){
					baseForm.doAction('submitJsonp',{
						submitEmptyText: false,//忽略emptyValue
						url: url,
						params: params,
						success: function(f, action){
							if (action.result.success) {
								Orchid.alert('消息', "操作成功!", function(){
									var gridPanel = win.grid;
									var store = gridPanel.getStore();
									store.reload();
									win.close();
								});
							}else{
								Orchid.alert('消息', action.result.msg||"系统出错,请联系管理员!", function(){
									win.close();
								});
							}
						},
						failure: function(f, action){
							Orchid.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
					});
				}else{
					btn.setDisabled(true);//点击“保存”按钮的时候，“保存”按钮置灰，不可点击
					form.submit({
						submitEmptyText: false,//忽略emptyValue
						url: url,
						params: params,
						success: function(f, action){
							if (action.result.success) {
								Orchid.alert('消息', "操作成功!", function(){
									var gridPanel = win.grid;
									var store = gridPanel.getStore();
									store.reload();
									win.close();
								});
							}else{
								Orchid.alert('消息', action.result.msg||"系统出错,请联系管理员!", function(){
									win.close();
								});
							}
						},
						failure: function(f, action){
							Orchid.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
					});
				}
			}
		},{
			text: '关闭',
			scope: this,
			hidden:me.isReadOnly,
			handler: this.close
		},{
			text: '关闭',
			scope: this,
			hidden:!me.isReadOnly,
			handler: this.close
		}];
		this.callParent(arguments);
	}
});